//
//  DetailCell.m
//  预购清单
//
//  Created by  on 2022/10/17.
//

#import "DetailCell.h"
#import "RecordCell.h"
#import "RecordHeader.h"
#import "RecordFooter.h"
@interface DetailCell()
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *tableViewH;
@property (nonatomic,strong) NSArray *array;
@property (copy, nonatomic)  NSString *key;

@end
@implementation DetailCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerNib:[RecordCell nib] forCellReuseIdentifier:[RecordCell ID]];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)setDict:(NSDictionary *)dict{
    _dict = dict;
    self.key = LS([[dict allKeys] firstObject]);
    self.array = [[dict allValues] firstObject];
    self.tableViewH.constant = self.array.count * 45 + 75 + 30;
    [self.tableView reloadData];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecordCell *cell = [tableView dequeueReusableCellWithIdentifier:[RecordCell ID]];
    cell.title = self.key;
    cell.model = self.array[indexPath.row];
    return  cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 75;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 16, 30)];
    RecordFooter *footer = (RecordFooter*)[RecordFooter loadFromXIB];
    footer.frame = view.bounds;
    footer.title = self.key;
    footer.array = self.array;
    [view addSubview:footer];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 16 , 75)];
    RecordHeader *header = (RecordHeader*)[RecordHeader loadFromXIB];
    header.title = self.key;
    header.frame = view.bounds;
    [view addSubview:header];
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

@end

